/**
 * room.op
 *
 */
 
// 命名空间 
if (typeof room != 'object')
	room = {}
 
 //定义变量保存用户名

room.currentUser = '';

/** 发送消息 */
room.sendMessage = function () {
	console.log('发送啦')
	var msg = $('#content').html().trim();
	console.log(msg)
	var op = msg.substr(0, 1);
	// 如果以$开头，则为系统消息
	if (op == '$') {
		room.sendSystemMessage(msg);
		return;
	}
	// 如果以@开头，则为私人信息
	if (op == '@') {
		var p = msg.indexOf(' ');
		if (p > 0) {
			room.sendPrivateMessage(msg);
			return;
		}
	}
	// 发送公共消息
	room.sendPublicMessage(msg);
}
/** 发送私人信息 */
room.sendPrivateMessage = function (msg) {
	to = msg.match(/(@[\u4e00-\u9fa5\w\-]+)/g);
	for (var i in to)
		to[i] = to[i].substr(1);
	socket.emit('private message', to, msg, function (err) {
		if (err) {
			room.showMessage('我', '刚才发送的消息“' + msg + '”不成功！（' + err + '）' , 'error');
		}
		else {
			room.showMessage('我', msg, 'own');
			room.clearInput();
		}
	});		
}
/** 发送公共消息 */
room.sendPublicMessage = function (msg) {
	socket.emit('public message', msg, function (err) {
		if (err) {
			room.showMessage('我', '刚才发送的消息“' + msg + '”不成功！（' + err + '）', 'error');
		}
		else {
			room.showMessage('我', msg, 'own');
			room.clearInput();
		}
	});
}


room.sendImgMessage = function(msg){
	socket.emit('img message',msg,function(err){
		if(err){
			console.log('aa')
			room.showMessage('我', '刚才发送的图片不成功！（' + err + '）', 'error');
		}else{
			room.showImgMessage('我',msg,'own');

		}
	})
}


/** 发送系统消息 */
room.sendSystemMessage = function (msg) {
	var args = msg.substr(1).split(/\s+/);
	title = '命令：' + args[0] + '\n';
	args[0] = args[0].toLowerCase();
	
	// 本地命令
	if (args[0] == 'clear') {
		room.clearMessage();
		room.clearInput();
		return;
	}
	
	// 远程命令
	socket.emit('command', args, function (err, ret) {
		if (err) {
			room.showMessage('系统', title + err, 'error');
		}
		else {
			room.showMessage('系统', title + ret, 'system');
			room.clearInput();
		}
	});
}

/** 显示一条消息 */
room.showMessage = function (from, msg, type) {
	var from = room.formatMessage(from);
	var msg = room.formatMessage(msg);
	
	var bstyle = '';

	if(type == 'system'){
		bstyle = 'btn-warning'
	}else{
		bstyle = 'btn-info'
	}


	var html = '\
	<div class="line ' + type + '">\
		<div class="message-header">\
			<button type="button" class="btn  btn-xs '+ bstyle +'">' + from + '</button>\
			<span style="font-size:12px">' + new Date().toLocaleString() + '</span>\
		</div>\
		<p class="breadcrumb" style="margin-top:4px">\
			' + msg + '\
		</p>\
	</div>';
		
	$('#lines').append(html);
}

/** 显示在线列表 */
room.showOnline = function (n) {
	var html = '';
	n.forEach(function (v) {
		html += '<li class="" style="height:50px;list-style:none" onclick="room.writePrivateMessage(\'' + v + '\')"><img class="media-object" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1020909558,4134438749&fm=21&gp=0.jpg" style="width:35px; height:35px;float:left"><h4 class="media-left" style="float:left">'+ v +'</h4></li>';
	});
	
	$('#nicknames').html(html);
}

/** 清空所有消息 */
room.clearMessage = function () {
	$('#lines .line').remove();
}

/** 清空输入框 */
room.clearInput = function () {
	$('#content').html('');
}

/** 在输入框中@某人 */
room.writePrivateMessage =function (n) {
	// TODO 用户名以@开始没做判断？

	if(n == this.currentUser){
		return false;
	}

	var $m = $('#content');

	if (n.substr(0, 1) != '@'){
		n = '@' + n;
	}


	$m.val(n + ' ' + $m.val());
}

/** 格式化消息 */ 
room.formatMessage = function (html) {
	/*html = html.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;')
		.replace(/\n/g, '<br>')
		.replace(/(@[\u4e00-\u9fa5\w\-]+)/gi, '<a href="#" onclick="room.writePrivateMessage(\'$1\');">$1</a>')
		.replace(/!((http|ftp|https|file):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\-\.\/?\@\%\!\&=\+\~\:\#\;\,]*)?)/ig, '<img src="$1">')
		.replace(/[\*]{2}([^\*]+)[\*]{2}/gi, '<b>$1</b>')
		.replace(/_([^_]+)_/gi, '<i>$1</i>');*/
	return html;
}

/** 显示当前用户 */
room.showCurrent = function(name){
	this.currentUser = name;
	$('#showusername').html(name);
}



/** 显示img消息 */
room.showImgMessage = function (from, msg, type) {
	var from = room.formatMessage(from);
	var msg = room.formatMessage(msg);
	var bstyle = '';

	if(type == 'system'){
		bstyle = 'btn-warning'
	}else{
		bstyle = 'btn-info'
	}

	
	var html = '\
		<div class="line ' + type + '">\
			<div class="message-header">\
				<button type="button" class="btn  btn-xs '+ bstyle +'">' + from + '</button>\
				<span style="font-size:12px">' + new Date().toLocaleString() + '</span>\
			</div>\
			<p class="breadcrumb" style="margin-top:4px"><img width="100" src="' + msg + '"/></p>\
		</div>';

	

	
	$('#lines').append(html);
}